import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { RegistPage } from '../regist/regist';

import {Http, Headers, RequestOptions} from '@angular/http';
import 'rxjs/add/operator/map';

import { HomePage } from '../home/home';

import 'rxjs/add/operator/toPromise';
import { People } from '../../app/people';
import { PeopleService } from '../../app/people.service';
import { loginURL } from '../../app/someConst';

// import { MyApp } from '../../app/app.component';
import { App } from 'ionic-angular';

import { PeopleDetailPage } from '../people-detail/people-detail';

/**
 * Generated class for the LoginPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  userName: string;
  password: string;
  user: People;

  private headers = new Headers({'Content-Type': 'application/json'});

  constructor(public navCtrl: NavController, 
    public navParams: NavParams, 
    public http: Http,
    public peopleService: PeopleService,
    // private appComponent: MyApp
    private app: App,
  ) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad LoginPage');
  }

  registAction(){
    this.navCtrl.push(RegistPage);
  }

  loginAction(){

    if(!this.userName){
      alert('用户名为空');
      return;
    }

    if(!this.password){
      alert('密码为空');
      return;
    }



    let body = new FormData();
    // 家乐参数
    // body.append('type', '1');
    // body.append('verify_code', this.password);
    // body.append('user_phone', this.userName);
    // 春生参数
    body.append('user_name', this.userName);
    body.append('user_pwd', this.password);

    // let URL = 'http://protest.xfzhi.com:88/userOperAction/logon';

    this.http.post(loginURL, body)

    this.http
        .post(loginURL, body)
        .map(res => res.json())
        .subscribe(
            data => {
            console.log(data);
            if(data.code == 200){
              this.user = data.result as People;
              this.peopleService.people = this.user;
              this.app.getRootNav().setRoot(PeopleDetailPage); 
            } else {
              // let message = data.message;
              alert(data.message);
              // return;
            }  

            },
            err => {
              console.log("ERROR!: ", err);
            }
        );
      // ----------------------------------

    // this.http
    //         .post(loginURL, JSON.stringify(body), {headers: this.headers})
    //         .toPromise()
    //         .then(res => {
    //           console.log(res);
    //           // this.user = res.json().result as People;
    //           // alert(this.user.user_name);
    //         }
            
    //       )
    //         .catch(this.handleError);

    //         alert(this.user.user_name);

    // -----------------------------------
  }
          

  private handleError(error: any): Promise<any> {
        console.error('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }



}
